﻿using System.Windows.Forms;

namespace Ast.Core.Forms
{
    public static class ListViewExtensions
    {
        public static void ApplyColumnSorter(this ListView listView)
        {
            // set sorter
            var listViewColumnSorter = new ListViewColumnSorter();
            listView.ListViewItemSorter = listViewColumnSorter;

            // set event
            listView.ColumnClick += (sender, e) =>
            {
                if (e.Column == listViewColumnSorter.SortColumn)
                {
                    listViewColumnSorter.Order = listViewColumnSorter.Order == SortOrder.Ascending
                        ? SortOrder.Descending
                        : SortOrder.Ascending;
                }
                else
                {
                    listViewColumnSorter.SortColumn = e.Column;
                    listViewColumnSorter.Order = SortOrder.Ascending;
                }
                listView.Sort();
            };
        }

        public static void AutoResizeColumns(this ListView listView)
        {
            foreach (ColumnHeader ch in listView.Columns)
            {
                ch.Width = -2;
            }
        }
    }
}